import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:m6/Untils/api.dart';

//aes加密
String aesEncode(String content) {
  try {
    final key = Key.fromUtf8(AESKey);
    final iv = IV.fromUtf8(AESIVStr);
    final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: null));
    final encrypted = encrypter.encrypt(content, iv: iv);
    return encrypted.base64;
  } catch (err) {
    print("aes encode error:$err");
    return content;
  }
}

//aes解密
String aesDecode(String content) {
  try {
    final key = Key.fromUtf8(AESKey);
    final iv = IV.fromUtf8(AESIVStr);
    final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
    final encrypted = Encrypted.fromUtf8(content);
    final str = encrypter.decrypt64(filtterContent(content), iv: iv);
    return str;
  } catch (err) {
    print("aes decode error:$err");
    return "";
  }
}

String filtterContent(String content) {
  var str = content;
  str = str.replaceAll(" ", "");
  return str;
}
